/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.client.communication;
import core.messages.Msg;
import core.threads.client.Client_Public_MsgBox;
import myTest.MYTest;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Invince
 */
public class T_Client_Sender extends Thread {
/*----------Attributs-------------------*/
    protected boolean stop;
    protected boolean suspend;

    //???????????底下的东西可以用Player_Connection代替掉
    protected String servAdd;
    protected int servPort;
    protected Socket socket;
    protected ObjectOutputStream objOutPut;
/*----------Public functions------------*/
    @Override
    public void run() {
        try {
            /**-----Init-----**/
            MYTest.println("Thread:T_Client_Sender start");
            /**-----Loop----**/
            while(!this.stop){
                if(!this.suspend){
                    if(!Client_Public_MsgBox.msgBox_Client_Send_Msg.isEmpty()){
                        MYTest.println("Thread:T_Client_Sender get msg");
                        Msg msgSend = Client_Public_MsgBox.msgBox_Client_Send_Msg.poll();
                        send(msgSend);
                    }
                }
            }
            /**----End-----**///这里End部分写在下面的finally里了
        } catch (IOException ex) {
            Logger.getLogger(T_Client_Sender.class.getName()).log(Level.SEVERE, null, ex);
       }finally{
            try {
                  this.objOutPut.close();//注意如果在send里open close的话，会把socket一起close掉
                  this.socket.close();
            } catch (IOException ex) {
                Logger.getLogger(T_Client_Sender.class.getName()).log(Level.SEVERE, null, ex);
            }
       }

    }

    public void send(Object obj) throws IOException{
        this.objOutPut.writeObject(obj);
        this.objOutPut.flush();
        MYTest.println("A message send.");
    }

    public void stopT(){
        synchronized(this){
            this.stop=true;
        }
    }

    public void suspendT(){
        synchronized(this){
            this.suspend=true;
        }
    }

    public void resumeT(){
        synchronized(this){
            this.suspend=false;
        }
    }
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Client_Sender(String servAdd,int servPort) throws UnknownHostException, IOException {
        synchronized(this){
            this.stop=false;
            this.suspend=false;
            this.servAdd = servAdd;
            this.servPort=servPort;
            this.socket=new Socket(servAdd,servPort);
            this.objOutPut=new ObjectOutputStream(this.socket.getOutputStream());
            MYTest.println("Thread:T_Client_Sender created");
        }
    }

/*----------Gets and sets---------------*/
    public String getServAdd() {
        return servAdd;
    }

    public void setServAdd(String servAdd) {
        this.servAdd = servAdd;
    }

    public int getServPort() {
        return servPort;
    }

    public void setServPort(int servPort) {
        this.servPort = servPort;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ObjectOutputStream getObjOutPut() {
        return objOutPut;
    }

    public void setObjOutPut(ObjectOutputStream objOutPut) {
        this.objOutPut = objOutPut;
    }
    
}
